package model

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/entity"
	"github.com/google/wire"
	"github.com/jinzhu/gorm"
)

// RoleSet 注入Role
var RolePrivilegeSet = wire.NewSet(wire.Struct(new(RolePrivilege), "*"))

// Role 角色存储
type RolePrivilege struct {
	DB *gorm.DB
	PrivilegeModel 			*Privilege
}


func (a *RolePrivilege) FindRoleHasPrivileges(ctx context.Context, roleID int64) entity.Privileges {
	var rolePrivilegeRelations entity.RolePrivilegeRelations
	entity.GetRoleDB(ctx, a.DB).Where("role_id = ?", roleID).Find(&rolePrivilegeRelations)
	privileges := make(entity.Privileges, 0)
	for _, rolePrivilegeRelation := range rolePrivilegeRelations {
		var privilege entity.Privilege
		entity.GetRoleDB(ctx, a.PrivilegeModel.DB).Where("id = ?", rolePrivilegeRelation.PrivilegeID).First(&privilege)
		privileges = append(privileges, privilege)
	}
	return privileges
}
